﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Application.Seedwork;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Spring.Context.Support;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.ReportModule;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class SupplierTypeAppService : BaseAppService<SupplierType, SupplierTypeSearch, ISupplierTypeRepository>, ISupplierTypeAppService
    {
        public override BoolGenericResult<SupplierType> AddOrUpdate(SupplierType entity)
        {
            Init();
            BoolGenericResult<SupplierType> result = new BoolGenericResult<SupplierType>();

            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Purchaser purchaser = new Purchaser(employee);

            SupplierType oldSupplierType = null;
            if (entity.Id >0)
            {
                oldSupplierType = this.Repository.Get(entity.Id);
            }

            if (oldSupplierType.IsNull())
            {
                IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
                result = purchaser.AddSupplierType(entity.Name, entity.EditUser);
                    return base.Add(result.Object);
            }
            else
            {
                result = purchaser.UpdateSupplierType(oldSupplierType, entity.Name, entity.EditUser);
                if (result.IsSuccess)
                    return base.Update(result.Object);
            }
            return result;
        }
    }
}
